
#include "TimeCtrl.h"
#include "../debug_new.h"

CTimeCtrl::CTimeCtrl(void)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

CTimeCtrl::~CTimeCtrl(void)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

CTimeCtrl::CTimeCtrl(UINT nID):CBmpBtn(nID)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

CTimeCtrl::CTimeCtrl(wstring strCaption,UINT nID)
:	CBmpBtn(strCaption,nID)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

CTimeCtrl::CTimeCtrl(DWORD dwStyle,wstring strCaption, const CRect &rc, UINT nID)
:	CBmpBtn(dwStyle, strCaption,rc, nID)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

CTimeCtrl::CTimeCtrl(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID)
:	CBmpBtn(dwStyle, strCaption, x, y, cx, cy, nID)
{
	m_bAutoGetSysTime = FALSE;
	m_nIconNum = 11;
	GetLocalTime(&m_time);
}

SYSTEMTIME CTimeCtrl::GetTime( )
{
	return m_time;
}

void	CTimeCtrl::SetTimeValue( int nInteger,int nDecimal )
{
	m_time.wMinute = nDecimal;
	m_time.wHour = nInteger;
}

void	CTimeCtrl::ReCalculatorRect()
{
	if ( m_pImgLstBtn && m_szIcon.cx==0 )
		m_pImgLstBtn->GetIconSize( m_szIcon );

	if ( m_szIcon.cx ==0 )
		return;
	
	m_rcControl.right = m_rcControl.left + m_szIcon.cx*5+6;
}

void CTimeCtrl::Paint(CDC* pDC)
{
	if(m_rcControl.IsRectEmpty())
	{
		return ;
	}

	if(!IsVisible())
	{
		return;
	}

	SYSTEMTIME sysTime;

	if(m_bAutoGetSysTime)
	{
		GetLocalTime(&sysTime);
	}
	else 
	{
		sysTime = m_time;
	}

	//TCHAR szBuffer[128];
	//swprintf(szBuffer,L"%.2d:%.2d",sysTime.wHour,sysTime.wMinute);
	//m_strCaption = szBuffer;
	//pDC->DrawText(szBuffer,m_strCaption.size(),m_rcControl,DT_SINGLELINE|DT_VCENTER);

	CSize sz;
	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->GetIconSize(sz);
	}

	CPoint pt = m_rcControl.TopLeft();

	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->Draw(pDC,sysTime.wHour / 10,pt);
	}
	pt.x += sz.cx+1;
	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->Draw(pDC,sysTime.wHour % 10,pt);
	}
	pt.x += sz.cx+1;
	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->Draw(pDC,10,pt);
	}
	pt.x += sz.cx+1;
	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->Draw(pDC,sysTime.wMinute / 10,pt);
	}
	pt.x += sz.cx+1;
	if(m_pImgLstBtn)
	{
		m_pImgLstBtn->Draw(pDC,sysTime.wMinute % 10,pt);
	}
}


void CTimeCtrl::SetAutoGetSysTime(BOOL bAutoGetSysTime)
{
	m_bAutoGetSysTime = bAutoGetSysTime;
}

void CTimeCtrl::SetTime(SYSTEMTIME time)
{
	m_time = time;
}

void CTimeCtrl::GetTime(SYSTEMTIME &time)
{
	time = m_time;
}

void	CTimeCtrl::IncMinute()
{
	m_time.wHour +=  (m_time.wMinute+1)/60;
	m_time.wMinute = (m_time.wMinute+1)%60;
}

void	CTimeCtrl::DecMinute()
{
	int nMinute = m_time.wMinute;
	if( nMinute>0 )
	{
		m_time.wMinute -= 1;
	}
	else
	{
		if ( m_time.wHour>0 )
		{
			m_time.wHour -= 1;
			m_time.wMinute = 59;
		}
		else
		{
			m_time.wHour = 0;
			m_time.wMinute = 0;
		}
	}
}
